var pathName=window.document.location.pathname;
var projectName=pathName.substring(1,pathName.substr(1).indexOf('/')+1);
/**
 * 验证码加载
 */
$.fn.extend({
	setGaptcha : function(){
		$(this).css({
			"width" : "50px",
			"height" : "22px",
			"background" : "#FFF url('auth/genCaptcha.do?dt="+(new Date()).valueOf()+"') no-repeat -1px -1px",
			"margin" : "5px 1px 4px 0px",
			"cursor": "pointer"
		})
//		.parent().css({"padding-right":"30px"})
		.prev("input[type='text']").css({"padding-right":"60px"});
		return this;
	}
});

function login(form){
	var $form = $(form);
	var url = $form.attr('action');
	url += "";
	var data = $form.serializeArray();
	var infoJson="{";
	for(var i=0;i<data.length;i++){
		var obj=data[i];
		if(obj.name=='userId'){
			if(obj.value.length<=0){
				document.getElementById("userId").focus();
				$(".login_msg").html('登录名不能为空');
				return false;
			}
			infoJson+="userId:'"+obj.value+"',";
		}
		if(obj.name=='userPwd'){
			if(obj.value.length<=0){
				document.getElementById("userPwd").focus();
				$(".login_msg").html('密码不能为空');
				return false;
			}
			infoJson+="userPwd:'"+obj.value+"',";
		}
		if(obj.name=='agentId'){
			if(obj.value.length<=0){
				document.getElementById("agentId").focus();
				$(".login_msg").html('合作商编号不能为空');
				return false;
			}
			infoJson+="agentId:'"+obj.value+"',";
		}
		if(obj.name=='captcha'&&obj.value.length<=0){
			document.getElementById("captcha").focus();
			$(".login_msg").html('验证码不能为空');
			return false;
		}
	}
	infoJson=infoJson.substring(0, infoJson.length-1)+"}";

	$.delCookie(projectName+"_infock");
	$.delCookie(projectName+"_info");
	if($('#ck_pwd').is(':checked')) {
		$.setCookie(projectName+"_infock","true");
		$.setCookie(projectName+"_info",utf16to8(infoJson));
	}
	data.push({name:"reqType",value:"ajax"});
	$.ajax({
		url: url,
		type:"POST",
		dataType:"json",
		data:data,
		success: function(result){	
			if(result.rspcod == 200){
				window.location=server_base_url + result.redirect;//跳到主页
			}else{
				$(".captcha").setGaptcha();
				$(".login_msg").html(result.rspmsg);
			}
		},
		error:function(XMLHttpRequest, textStatus){
			alert("网络异常代码："+XMLHttpRequest.status+",异常描述："+textStatus);
		}
	});
	
	return false;
}

$(document).ready(function() {
	if($.getCookie(projectName+"_infock")){
		var info=eval("("+utf8to16($.getCookie(projectName+"_info"))+")");
		$("#userId").val(info.userId);
		$("#userPwd").val(info.userPwd);
		$("#agentId").val(info.agentId);
		$("#ck_pwd").attr("checked", $.getCookie(projectName+"_infock"));
	}
	$(".captcha").on("click",function(){
		$(this).setGaptcha();
	}).setGaptcha();
});

function utf16to8(str) {
    var out, i, len, c;
    out = "";
    len = str.length;
    for (i = 0; i < len; i++) {
        c = str.charCodeAt(i);
        if ((c >= 0x0001) && (c <= 0x007F)) {
            out += str.charAt(i)
        }
        else if (c > 0x07FF) {
            out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
            out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
            out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
        }else {
            out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
            out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
        }
    }
    return out;
}

function utf8to16(str) {
    var out, i, len, c;
    var char2, char3;
    out = "";
    len = str.length;
    i = 0;
    while (i < len) {
        c = str.charCodeAt(i++);
        switch (c >> 4) {
        case 0:
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
        case 7:
            out += str.charAt(i - 1);
            break;
        case 12:
        case 13:
            char2 = str.charCodeAt(i++);
            out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
            break;
        case 14:
            char2 = str.charCodeAt(i++);
            char3 = str.charCodeAt(i++);
            out += String.fromCharCode(((c & 0x0F) << 12) | ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0));
            break;
        }
    }
    return out;
}